//
//  ArrayStudy.swift
//  Swift3.0_Study
//
//  Created by chengfeng on 2016/9/27.
//  Copyright © 2016年 zhaochengfeng. All rights reserved.
//

import Cocoa

class ArrayStudy: NSObject {
    /*
     一组同类型的值的组合, 根据组合的整体特性分为:
     有序可重复 - 数组(Array)
     无序不重复 - Set
     无序可重复,但每个值有唯一的键(Key) - 字典(Dictionary)
     ☆批量处理集合中的元素, 可以使用 for in循环
     */
    
    //数组
    //定义: Array<类型> 或 [类型]

    func start() -> Void {
        
        //初始化1
        let array: [Int] = [1, 2, 3]
        print(array)
        
        //初始化2
        let arr = [Int](repeatElement(10, count: 3))
        print("arr = \(arr), count = \(arr.count)")
        
        //初始化3
        let array2 = Array(1...10)
        print(array2)
        
        var places = ["beijing","shanghai","guangzhou","shenzhen"]
        
        //①元素计数:count, 空否: isEmpty
        let count =  places.count
        let isEmpty = places.isEmpty
        
        print("\(count), \(isEmpty)")
        
        //添加: append, 添加一个同类型数组 +=
        places .append("hangzhou")
        
        //另一个相同类型的数组
        let haiwaiPlace = ["NewYork","London","Sao paolu"]
        
        //+=合并两个相同类型的数组
        places += haiwaiPlace
        
        //③获取元素 : 数组名[索引], ⚠️索引总是从0开始
        print(places[0])
        
        //④↙️插入: insert
        //插入单个
        places.insert("shen qiu", at: 0)
        
        //批量插入
        places.insert(contentsOf: ["1", "2"], at: 1)
        
        print("插入后的数组 \(places)")
        
        //⑤↗️移除: remove
        places.remove(at: 0)
        places.removeSubrange(0 ..< 5)
//        places.index(of: "guaungzhou")
        print("移除后的数组 \(places)")
        
        print(places.contains("London")) 
    }
}
